package com.handpay.nocardpay.baselibrary.setting.view.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.widget.GestureLockDisplayView;
import com.handpay.nocardpay.baselibrary.main.widget.GestureLockView;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;


/**
 * Created by lyang on 2017/10/16.
 */

public class GestureLockVerifyFragment extends BaseFragment {
    private GestureLockDisplayView gesture_lock_display_view;
    private GestureLockView gesture_lock_view;
    private TextView gesture_status_tv;
    private int limitNum = 4;

    public static GestureLockVerifyFragment newInstance() {
        Bundle args = new Bundle();

        GestureLockVerifyFragment fragment = new GestureLockVerifyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gesture_lock_verify;
    }

    @Override
    public void initView(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        gesture_status_tv = (TextView) view.findViewById(R.id.gesture_status_tv);
        gesture_lock_display_view = (GestureLockDisplayView) view.findViewById(R.id.gesture_lock_display_view);
        gesture_lock_view = (GestureLockView) view.findViewById(R.id.gesture_lock_view);
        gesture_lock_view.setKey(ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD));
        gesture_lock_view.setOnGestureFinishListener(new GestureLockView.OnGestureFinishListener() {
            @Override
            public void OnGestureFinish(boolean success, String key) {
                if (success) {
                    gesture_lock_display_view.setAnswer(true,key);
                    GestureLockVerifyFragment.this.setFragmentResult(SettingGesturePWDFragment.RESULT_VERIFY_GESTURE_PWD_SUCCESS, null);
                    GestureLockVerifyFragment.this.pop();
                    clearDisplayView();
                } else {
                    gesture_lock_display_view.setAnswer(false,key);

                    if (key.length() >= limitNum) {
                        showStatusAndClearDisplayView(getString(R.string.gesture_verify_error));
                    } else {
                        showStatusAndClearDisplayView(getString(R.string.gesture_other_login_limit_tips));
                    }
                }
            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.setting_gesture_pwd);
    }

    private void showStatusAndClearDisplayView(String status) {
        gesture_status_tv.setTextColor(getResources().getColor(R.color.red_color));
        gesture_status_tv.setText(status);
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                gesture_status_tv.setText(getString(R.string.gesture_status_old_pwd));
                gesture_status_tv.setTextColor(getResources().getColor(R.color.text_color_normal));
                gesture_lock_display_view.clearAnswer();

            }
        };
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    private void clearDisplayView() {
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                gesture_lock_display_view.clearAnswer();

            }
        };
        handler.sendEmptyMessageDelayed(1, 1000);
    }
}
